﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using vensoft.Entities;

namespace vensoft.DataAccess
{
    public class ClienteDataAccess: MasterDataAcess,IClienteDataAccess
    {
        public ClienteDataAccess()
            : base()
        {
        }

        public List<Entities.Cliente> GetAllFromCliente()
        {
            SqlCommand commando = base.CreateSelectCommand("GetAllFromCliente");
            return SelectObjectFactory(commando);
        }

        private List<Cliente> SelectObjectFactory(SqlCommand commando)
        {
            List<Cliente> lista = new List<Cliente>();

            using(SqlDataReader reader = base.ExecuteReader(commando))
            {
                while (reader.Read())
                {
                    Cliente cliente = new Cliente()
                    {
                        codigo_Cliente = reader.GetInt32(0),
                        razon_social = reader.GetString(1),
                        DNI_RUC = reader.GetString(7),
                        AP_Materno = reader.GetString(4),
                        tipo = reader.GetString(6),
                        AP_Paterno = reader.GetString(5),
                        telefono = reader.GetString(2),
                        direccion = reader.GetString(3)
                    };

                    lista.Add(cliente);
                }
            }
            
            return lista;
        }

        public List<Entities.Cliente> GetFromClienteByNombre(string criterio)
        {
            SqlCommand command = base.CreateSelectCommand("GetFromClienteByNombre");
            command.Parameters.AddWithValue("@nombre_razsocial", criterio);
            return SelectObjectFactory(command);
        }

        public void EliminarCliente(Cliente cliente)
        {
            SqlCommand command = base.CreateCommand("EliminarCliente");
            base.AddInParameter(command, "DNI_RUC", cliente.DNI_RUC);
            base.ExecuteCommand(command);
        }


        public void RegistarCliente(Cliente cliente)
        {
            SqlCommand command = base.CreateCommand("RegistarCliente");
            AddParametersp(cliente, command);
        }

        private void AddParametersp(Cliente cliente, SqlCommand command)
        {
            base.AddInParameter(command, "codigoCliente", cliente.codigo_Cliente);
            base.AddInParameter(command, "razon_social", cliente.razon_social + ' ' + cliente.AP_Paterno + ' ' + cliente.AP_Materno);
            base.AddInParameter(command, "telefono ", cliente.telefono);
            base.AddInParameter(command, "direccion ", cliente.direccion);
            base.AddInParameter(command, "AP_Materno ", cliente.AP_Materno);
            base.AddInParameter(command, "AP_Paterno ", cliente.AP_Paterno);
            base.AddInParameter(command, "Tipo ", cliente.tipo);
            base.AddInParameter(command, "DNI_RUC", cliente.DNI_RUC);

            base.ExecuteCommand(command);
        }
    }
}
